package com.app.micaihu.view.main.game.view;

import android.content.Context;
import android.graphics.Canvas;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.micaihu.R;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.ValueAnimator;

import java.text.DecimalFormat;
import java.text.NumberFormat;

/**
 * Created by Guo on 2017/3/15.
 * <p>
 * 游戏评分的进度
 */

public class ScorePorgressView extends LinearLayout implements ViewTreeObserver.OnGlobalLayoutListener,OnViewAddedListener {
    private Context context;
    private String level;
    private float percent;
    private TextView tvLevel;
    private TextView tvPercent;
    private View progress;
    private boolean onChanged;
    private OnViewAddedListener onViewAddedListener;
    private final int AnimalDuration = 1000;
    private DecimalFormat df;
    private int matchWidth;


    public ScorePorgressView(Context context, String level, float percent) {
        super(context);
        this.context = context;
        this.level = level;
        this.percent = percent;
//        LogUtils.e("gzq", "new ScoreP()  percent = "+percent);
        init();

    }

    public ScorePorgressView(Context context) {
        super(context);
        this.context = context;

        init();
    }

    public ScorePorgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    public ScorePorgressView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }


    void setOnViewAddedListener(OnViewAddedListener onViewAddedListener){

        this.onViewAddedListener = onViewAddedListener;
    }
    private void init() {
        inflate(this.context, R.layout.view_game_score_view, this);
        tvLevel = (TextView) findViewById(R.id.level);
        tvPercent = (TextView) findViewById(R.id.percent);
        progress = findViewById(R.id.loading);

        ViewTreeObserver viewTreeObserver = getViewTreeObserver();
        viewTreeObserver.addOnGlobalLayoutListener(this);

        setOnViewAddedListener(this);

        //.00就表示保留后两位数
//        df = new DecimalFormat(".0");

        df = (DecimalFormat) NumberFormat.getPercentInstance(); //百分数
//        System.out.println(nf.format(0.476354));
        df.applyPattern("0.0%") ;
//        System.out.println(nf.format(0.476354));
    }



    public void setLevel(String level) {
        this.level = level;
    }

    public void setPercent(float percent) {
        this.percent = percent;
    }

    @Override
    public void onGlobalLayout() {
        matchWidth = progress.getWidth();
        ViewGroup.LayoutParams layoutParams = progress.getLayoutParams();
        if(percent < 0.001f){
            layoutParams.width = 1;
        }else{
            int v = (int) (matchWidth * percent);
            layoutParams.width = v;
        }
        progress.setLayoutParams(layoutParams);
        progress.invalidate();
        tvPercent.setText("0.00%");
        if(percent < 0.001f){
            progress.setVisibility(View.GONE);
        }else{
            progress.setVisibility(View.VISIBLE);
        }
        tvLevel.setText(level);
        ViewTreeObserver viewTreeObserver = getViewTreeObserver();
        viewTreeObserver.removeOnGlobalLayoutListener(this);

        if(onViewAddedListener !=null){
            onViewAddedListener.onViewAdded();
        }


    }


    @Override
    public void onViewAdded() {
        if(progress != null && tvPercent != null){
            final ViewGroup.LayoutParams layoutParams = progress.getLayoutParams();

            final int width = layoutParams.width;
            if(progress.getVisibility() == View.VISIBLE){
//                LogUtils.e("gzq", "width = " + width);

//                ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, width);
                ValueAnimator valueAnimator = ValueAnimator.ofInt(0, width);
                valueAnimator.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        if(progress != null){
                           progress.setBackgroundColor(getResources().getColor(R.color.common_orange_color_60));
                        }
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        if(tvPercent != null){

                            String format = df.format(percent);
                            tvPercent.setText(format);
//                            LogUtils.e("gzq", "final = "+(percent * 100)+"   round = "+round + "  i = "+i);
                        }
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {

                    }
                });

                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        int animatedValue = (int) animation.getAnimatedValue();
                        layoutParams.width =  animatedValue;
//                        LogUtils.e("gzq", "(int) animatedValue) "+(int) animatedValue);
                        progress.setLayoutParams(layoutParams);
//                       float v =  (animatedValue / matchWidth) * 100;
//                        String format = df.format(v);
                        //1 表示保留1位小数
//                        tvPercent.setText(format + "%");

                        String format1 = df.format(((float)animatedValue / matchWidth));
                        tvPercent.setText(format1);
                    }

                });
                valueAnimator.setDuration(AnimalDuration);
                valueAnimator.start();
            }


        }

    }
}
